﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using TestSys.Models;

namespace TestSys.Controllers
{
    public class UserController : Controller
    {
        // GET: /User/

        [Authorize]
        public ActionResult Tests()
        {
            DataManager dataManager = new DataManager();
            generalinfo currentUser = dataManager.GetUser(User.Identity.Name);
            List<tests> resList = new List<tests>();
            foreach(var item in currentUser.userinfo.groups.testsforgroups)
                resList.Add(dataManager.entity.tests.Select(x=>x).Where(x=>x.TestID == item.TestID).FirstOrDefault());
            return View(resList);
        }

        public ActionResult Results()
        {
            DataManager dataManager = new DataManager();
            generalinfo currentUser = dataManager.GetUser(User.Identity.Name);
            return View(currentUser.userinfo.testsresults);
        }

        public ActionResult OpenTest(int? id)
        {
            DataManager dataManager = new DataManager();
            List<rootQuestion> questions = null;
            if (id.HasValue)
            {
                questions = dataManager.LoadTest(id.Value);
                if (questions == null)
                {
                    ViewBag.Message = "Couldn`t load this test or it is empty";
                    return View("ErrorPage");
                }
                int nonNull_id = id.Value;
                ViewBag.TestID = nonNull_id;
                return View(questions);
            }
            else
                return View("ErrorPage", "Bad test id, try again to open it");
        }

        [HttpPost]
        public ActionResult SubmitTest(FormCollection form)
        {
            DataManager dataManager = new DataManager();
            generalinfo currentUser = dataManager.GetUser(User.Identity.Name);
            //logic
            int TestID;
            if (int.TryParse(Request.Form["id"], out TestID))
            {
                List<rootQuestion> test = dataManager.LoadTest(TestID);
                string[] corr = new string[test.Capacity];
                string[] user = new string[test.Capacity];
                StringBuilder temp = new StringBuilder();
                for (int i = 0; i < test.Capacity; i++)
                {
                    temp.Clear();
                    for (int j = 0; j < test[i].answ.Length; j++)
                        if (Request.Form[String.Format("checkbox{0}{1}", i, j)] != "false")
                            temp.Append('1');
                        else
                            temp.Append('0');
                    corr[i] = test[i].ans;
                    user[i] = temp.ToString();
                }
                int result = dataManager.CheckTest(corr, user);
                if (dataManager.SubmitNewResult(currentUser.UID, TestID, result))
                    return Redirect("Results");
                else
                    return View("ErrorPage", "failed to save your result");
            }
            return View("ErrorPage", "unexpected error during submitting a test, corrupt testID value of test");
        }

        
    }
}
